import 'package:flutter/material.dart';
import 'package:flutter_baselib/adapter/log_adapter.dart';
import 'package:flutter_baselib/utils/log_util.dart';

class LogUi extends StatefulWidget {
  const LogUi({super.key});

  @override
  State<LogUi> createState() => _LogUiState();
}

class _LogUiState extends State<LogUi> {
  late final LoggerUtil log;

  final LoggerAdapter logger = LoggerAdapter(Logger(
      printer: PrettyPrinter(
    methodCount: 2, // Number of method calls to be displayed
    errorMethodCount: 8, // Number of method calls if stacktrace is provided
    lineLength: 120, // Width of the output
    colors: true, // Colorful log messages
    printEmojis: true, // Print an emoji for each log message
    // Should each log print contain a timestamp
    dateTimeFormat: DateTimeFormat.onlyTimeAndSinceStart,
  )));
  late final CherriLoggerAdapter cherriLoggerAdapter;

  @override
  void initState() {
    super.initState();
    log = LoggerUtil(logger: logger);
    final cherriLog = CherriLog.init(
        options: CherriOptions()
          ..logLevelRange = CherriLogLevelRanges.all
          ..useBuffer = false);
    cherriLog.logTo(CherriConsole());
    cherriLoggerAdapter = CherriLoggerAdapter(cherriLog);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Log'),
      ),
      body: Column(
        children: [
          ElevatedButton(
            onPressed: () => log.adapter = logger,
            child: const Text('init logger'),
          ),
          ElevatedButton(
            onPressed: () => log.adapter = cherriLoggerAdapter,
            child: const Text('init cherri logger'),
          ),
          ElevatedButton(
            onPressed: () {
              log.e('error');
              log.w('warn');
              log.i('info');
              log.d('debug');
              log.f('fatal');
            },
            child: const Text('log'),
          ),
        ],
      ),
    );
  }
}
